﻿using System;
using System.IO;

namespace LearnFile6
{
    class Program
    {
        static void Main(string[] args)
        {
            // 文件路径
            // 文件和文件夹的路径操作都在Path类中。另外还可以用Environment类，里面包含环境和程序的信息。
            string dirPath = @"E:\CSharpFolder\CSharp\runoob.com\Basics";
            string filePath =
                @"E:\CSharpFolder\CSharp\runoob.com\Basics\readme.txt";
            Console.WriteLine("<<<<<<<<<<<{0}>>>>>>>>>>", "文件路径");

            //获得当前路径
            Console.WriteLine(Environment.CurrentDirectory);

            //文件或文件夹所在目录
            Console.WriteLine(Path.GetDirectoryName(filePath)); //E:\CSharpFolder\CSharp\runoob.com\Basics
            Console.WriteLine(Path.GetDirectoryName(dirPath)); //E:\CSharpFolder\CSharp\runoob.com

            //文件扩展名
            Console.WriteLine(Path.GetExtension(filePath)); //.txt

            //文件名
            Console.WriteLine(Path.GetFileName(filePath)); //readme.txt
            Console.WriteLine(Path.GetFileName(dirPath)); //Basics
            Console.WriteLine(Path.GetFileNameWithoutExtension(filePath)); //readme

            //绝对路径
            Console.WriteLine(Path.GetFullPath(filePath)); //E:\CSharpFolder\CSharp\runoob.com\Basics\readme.txt
            Console.WriteLine(Path.GetFullPath(dirPath)); //E:\CSharpFolder\CSharp\runoob.com\Basics

            //更改扩展名
            Console.WriteLine(Path.ChangeExtension(filePath, ".jpg")); //E:\CSharpFolder\CSharp\runoob.com\Basics\readme.jpg

            //根目录
            Console.WriteLine(Path.GetPathRoot(dirPath)); //E:\

            //生成路径
            Console
                .WriteLine(Path
                    .Combine(new string[] {
                        @"D:\",
                        "BaseDir",
                        "SubDir",
                        "TestFile.txt"
                    })); //D:\BaseDir\SubDir\TestFile.txt

            //生成随即文件夹名或文件名
            Console.WriteLine(Path.GetRandomFileName());

            //创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径
            Console.WriteLine(Path.GetTempFileName());

            //返回当前系统的临时文件夹的路径
            Console.WriteLine(Path.GetTempPath());

            //文件名中无效字符
            Console.WriteLine(Path.GetInvalidFileNameChars());

            //路径中无效字符
            Console.WriteLine(Path.GetInvalidPathChars());

            Console.WriteLine("==================================");

            // 文件属性操作
            // File类与FileInfo都能实现。静态方法与实例化方法的区别！
            //use File class
            Console.WriteLine(File.GetAttributes(filePath));
            File
                .SetAttributes(filePath,
                FileAttributes.Hidden | FileAttributes.ReadOnly);
            Console.WriteLine(File.GetAttributes(filePath));

            //user FilInfo class
            FileInfo fi = new FileInfo(filePath);
            Console.WriteLine(fi.Attributes.ToString());
            fi.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly; //隐藏与只读
            Console.WriteLine(fi.Attributes.ToString());

            //只读与系统属性，删除时会提示拒绝访问
            fi.Attributes = FileAttributes.Archive;
            Console.WriteLine(fi.Attributes.ToString());
        }
    }
}
